#include "EditorCore/History.h"
#include "Common/Exceptions/NullPointerException.h"

using namespace cmn;

namespace editor_core {

	History::History() :
		m_currentActionIndex( 0 )
	{}

	void History::Do( IAction* action ) {
		if( !action )
			throw NullPointerException( "Cannot DO null action" );

		if( action->Do() && action->GetType() == ActionType::Undoable ) {
			for( std::size_t i = m_currentActionIndex; i < m_actionList.size(); ++i ) {
				IAction* lostAction = m_actionList[i];
				delete lostAction;
			}
			m_actionList.erase( m_actionList.begin() + m_currentActionIndex, m_actionList.end() );

			m_actionList.push_back( action );
			m_currentActionIndex++;
		}
	}

	void History::Undo() {
		if( m_currentActionIndex > 0 ) {
			IAction* action = m_actionList[--m_currentActionIndex];
			action->Undo();
		}
	}

	void History::Redo() {
		if( m_currentActionIndex < m_actionList.size() ) {
			IAction* action = m_actionList[m_currentActionIndex++];
			action->Redo();
		}
	}

}
